package vip.xiaonuo.supplier.modular.setting.reporttype.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.setting.reporttype.entity.PublicReportType;
import vip.xiaonuo.supplier.modular.setting.reporttype.mapper.PublicReportTypeMapper;
import vip.xiaonuo.supplier.modular.setting.reporttype.param.PublicReportTypeAddParam;
import vip.xiaonuo.supplier.modular.setting.reporttype.param.PublicReportTypeEditParam;
import vip.xiaonuo.supplier.modular.setting.reporttype.param.PublicReportTypeIdParam;
import vip.xiaonuo.supplier.modular.setting.reporttype.param.PublicReportTypePageParam;
import vip.xiaonuo.supplier.modular.setting.reporttype.service.PublicReportTypeService;

import java.util.List;

/**
 * 报表类型Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/04/03 09:31
 **/
@Service
public class PublicReportTypeServiceImpl extends ServiceImpl<PublicReportTypeMapper, PublicReportType> implements PublicReportTypeService {

    @Override
    public Page<PublicReportType> page(PublicReportTypePageParam publicReportTypePageParam) {
        QueryWrapper<PublicReportType> queryWrapper = new QueryWrapper<PublicReportType>().checkSqlInjection();
        if(ObjectUtil.isAllNotEmpty(publicReportTypePageParam.getSortField(), publicReportTypePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(publicReportTypePageParam.getSortOrder());
            queryWrapper.orderBy(true, publicReportTypePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(publicReportTypePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(PublicReportType::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(PublicReportTypeAddParam publicReportTypeAddParam) {
        PublicReportType publicReportType = BeanUtil.toBean(publicReportTypeAddParam, PublicReportType.class);
        this.save(publicReportType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(PublicReportTypeEditParam publicReportTypeEditParam) {
        PublicReportType publicReportType = this.queryEntity(publicReportTypeEditParam.getId());
        BeanUtil.copyProperties(publicReportTypeEditParam, publicReportType);
        this.updateById(publicReportType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<PublicReportTypeIdParam> publicReportTypeIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(publicReportTypeIdParamList, PublicReportTypeIdParam::getId));
    }

    @Override
    public PublicReportType detail(PublicReportTypeIdParam publicReportTypeIdParam) {
        return this.queryEntity(publicReportTypeIdParam.getId());
    }

    @Override
    public PublicReportType queryEntity(String id) {
        PublicReportType publicReportType = this.getById(id);
        if(ObjectUtil.isEmpty(publicReportType)) {
            throw new CommonException("报表类型不存在，id值为：{}", id);
        }
        return publicReportType;
    }
}
